﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Table_pet
{
    public static class SystemFunction
    {
        private static Dictionary<string, DateTime> sentNotifications = new Dictionary<string, DateTime>();
        private static PerformanceCounter cpuCounter;



        public static void ShowNotification(string title, string message)
        {
            // 生成唯一的通知标识符
            string notificationKey = title + "|" + message;

            // 检查是否已发送过相同的通知
            if (sentNotifications.ContainsKey(notificationKey))
            {
                DateTime lastSentTime = sentNotifications[notificationKey];
                TimeSpan elapsed = DateTime.Now - lastSentTime;

                // 10分钟内不发送相同通知
                if (elapsed.TotalMinutes < 10)
                {
                    return;
                }
            }

            // 发送新的通知
            NotifyIcon notifyIcon = new NotifyIcon();
            notifyIcon.Icon = SystemIcons.Information;
            notifyIcon.Visible = true;

            notifyIcon.BalloonTipTitle = title;
            notifyIcon.BalloonTipText = message;
            notifyIcon.ShowBalloonTip(5000);

            // 存储通知的发送时间
            sentNotifications[notificationKey] = DateTime.Now;

            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }


        public static float GetCPUUsage()
        {
            if (cpuCounter == null)
            {
                cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            }
            float cpuUsage = cpuCounter.NextValue();
            return cpuUsage;
        }

        //获取内存使用情况
        public static float GetMemoryUsage()
        {
            PerformanceCounter memCounter = new PerformanceCounter("Memory", "% Committed Bytes In Use");
            return memCounter.NextValue();
        }

        //检测剩余电量
        public static int UpdateInfo()
        {
            try
            {
                // 创建查询对象，获取Win32_Battery类的实例
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Battery");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

                // 获取所有电池实例
                ManagementObjectCollection batteryCollection = searcher.Get();

                foreach (ManagementObject battery in batteryCollection)
                {
                    // 获取电池电量属性值
                    int batteryPercentage = Convert.ToInt32(battery["EstimatedChargeRemaining"]);
                    return batteryPercentage;
                }
            }
            catch (Exception ex)
            {
                // 处理异常
                Console.WriteLine("Error: " + ex.Message);
            }

            return -1; // 如果无法获取电池电量，返回-1
        }

    }
}
